<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>地区福利列表</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" />
	<link rel="stylesheet" href="//at.alicdn.com/t/font_tnyc012u2rlwstt9.css" media="all" />
</head>
<body class="childrenBody">
	<blockquote class="layui-elem-quote news_search">
		<form class="layui-form">
        <div class="layui-inline">
            <label class="layui-form-label">区域</label>
            <div class="layui-input-block">
                <select name="regionId">
                	<option value="">--请选择--</option>
				</select>
            </div>
        </div>
        <div class="layui-inline">
            <a class="layui-btn search_btn" lay-submit="" lay-filter="queryWelfare">查询</a>
        </div>
        <div class="layui-inline">
           <a class="layui-btn layui-btn-normal welfaresAdd_btn">添加地市福利</a>
        </div>
    </form>
		
	</blockquote>
	<div class="layui-form news_list">
	  	<table class="layui-table" id="welfareList" lay-filter="welfares"></table>
	</div>
	<div id="page"></div>
	<script type="text/javascript" src="js/jquery-1.11.2.min.js"></script>
	<script type="text/javascript" src="plugins/layui/layui.all.js"></script>
	<script type="text/javascript" src="js/user/newsList.js"></script>
	<script type="text/html" id="barDemo">
		<a class="layui-btn layui-btn-xs" lay-event="edit"><i class="iconfont icon-edit"></i>编辑</a>
	    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon"></i>删除</a>
	  </script>
	<script type="text/javascript">
	//重新渲染表单
	function renderForm(){
	  layui.use('form', function(){
	   var form = layui.form;
	   form.render();
	  });
	 }
	
	layui.use(['table','form'], function(){
	    var table = layui.table;
	    var form = layui.form;

	    //方法级渲染
	    var tableIns=table.render({
	        elem: '#welfareList'
	        ,url: '/welfares'
	        ,cols: [[
	            {checkbox: true, fixed: true}
	            ,{field:'id', title: 'ID', width:'10%', sort: true, fixed: true}
	            ,{field:'regionName', title: '地区名称',  width:'20%', sort: true}
	            ,{field:'welfareName', title: '福利名称',  width:'20%', sort: true}
	            ,{field:'welfarePrice', title: '福利价格',  width:'20%', sort: true}
	            ,{title: '操作', align:'center', toolbar: '#barDemo'} //这里的toolbar值是模板元素的选择器
	            /*  ,{field:'date', title: '日期', width:180}
	            ,{field:'datatype', title: '日期类型', width:100}*/

	        ]]
	        ,id: 'testReload'
	        ,page: true
//	        ,height: 600
	    });
	    
	    form.on("submit(queryWelfare)", function (data) {
	        var regionId = data.field.regionId;
	        //表格重新加载
	        tableIns.reload({
	            where:{
	            	regionId:regionId
	            }
	        });

	        return false;
	    });
	    
	    table.on('tool(welfares)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
	    	  var data = obj.data; //获得当前行数据
	    	  var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
	    	  var tr = obj.tr; //获得当前行 tr 的DOM对象
	    	 
	    	if(layEvent === 'del'){ //删除
	    	    layer.confirm('真的删除该福利吗', function(index){
	    	      layer.close(index);
	    	      //向服务端发送删除指令
	    	      var ids=obj.data.id;
	    	      $.ajax({  
	    	            type: "DELETE",
	    	            url:"/welfares/delete/"+ids,
	    	            success: function(resp) {
	    	            	if(resp.code==2000){
	    	            		top.layer.close(index);
	    	          			top.layer.msg("删除成功");
                                obj.del();
	    	            	}

	    	            } ,

	    		 		 error: function(request) {  
	    		             alert("请求错误");
	    		         } 
	    	        }); 
	    	    });
	    	  } else if(layEvent === 'edit'){ //编辑
	    	     var ids=obj.data.id;
	    	     editWelfare(ids);
	    	  }
	    	});
	});
	
	$(window).one("resize",function(){
		$(".welfaresAdd_btn").click(function(){
			var index = layui.layer.open({
				title : "添加地市福利",
				type : 2,
				content : "welfares/welfaresAdd.html",
				success : function(layero, index){
					
				}
			})			
			layui.layer.full(index);
		})
	}).resize();
	
	 var regionsList;
	  $.ajax({
         type : "GET",
         url: '/regions/getAll',
         dataType:'json',
      	  async:false,
         success: function(data){
         	var list=data.data;
         	regionsList=list;
         	var html='';
             for(var i=0;i<list.length;i++){	
                 html += "<option value='"+list[i].id+"'>"+list[i].regionName+"</option>"
             }
             $("select[name='regionId']").append(html);
             renderForm();//表单重新渲染，要不然添加完显示不出来新的option
         }
     });
	  
	  
	  function editWelfare(id){
	        var index = layui.layer.open({
	            title: "编辑地市福利",
	            type: 2,
	            content: "welfares/welfaresEdit.html?id="+id,
	            success: function (layero, index) {

	            }
	        });

	        //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
	        $(window).resize(function () {
	            layui.layer.full(index);
	        });
	        layui.layer.full(index);
	  }
	</script> 
</body>
</html>